package com.bytezero.login_test.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bytezero.login_test.R;
import com.bytezero.login_test.bean.Goods;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.List;

/**
 * @author Bytezero_zhenglei!
 * @package com.bytezero.login_test.home.adapter
 * @fileName
 * @data 2022/1/27 16:52
 * @description
 * @TODO
 * @email 420498246@qq.com
 */
public class HomeRecycleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private final List<Goods> data;
    private final LayoutInflater layoutInflater;

    public HomeRecycleViewAdapter(Context context, List<Goods> data) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.data = data;

    }

    public void setGoods(List<Goods> data) {
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();

    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = layoutInflater.inflate(viewType, parent, false);
        if (viewType == R.layout.home_recycler_text_image) {
            return new MultiViewHolder(view);
        }

        return new SigleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Goods goods = data.get(position);
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case R.layout.home_recycler_banner:
                ((Banner) holder.itemView).setAdapter(new BannerImageAdapter<String>(goods.getBanners()) {
                    @Override
                    public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                        Glide.with(holder.itemView)
                                .load(data)
                                .apply(RequestOptions.centerCropTransform())
                                .into(holder.imageView);
                    }
                }).addBannerLifecycleObserver((LifecycleOwner) context)
                        .setIndicator(new CircleIndicator(context));
                break;

            case R.layout.home_recycler_image:
                Glide.with(holder.itemView)
                        .load(goods.getImageUrl())
                        .apply(RequestOptions.centerCropTransform())
                        .into((ImageView) holder.itemView);

                break;

            case R.layout.home_recycler_text:
                ((TextView) holder.itemView).setText(goods.getText());

                break;
            case R.layout.home_recycler_text_image:

                MultiViewHolder multiViewHolder = (MultiViewHolder) holder;
                multiViewHolder.textView.setText(goods.getText());

                Glide.with(holder.itemView)
                        .load(goods.getImageUrl())
                        .apply(RequestOptions.centerCropTransform())
                        .into(multiViewHolder.imageView);
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        Goods goods = data.get(position);
        if (goods.getBanners() != null) {
            return R.layout.home_recycler_banner;

        } else if (goods.getImageUrl() == null) {

            //显示文字
            return R.layout.home_recycler_text;

        } else if (goods.getText() == null) {

            //显示图片
            return R.layout.home_recycler_image;
        } else {
            //文字+图片
            return R.layout.home_recycler_text_image;
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class SigleViewHolder extends RecyclerView.ViewHolder {

        public SigleViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    class MultiViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView imageView;

        public MultiViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text2);
            imageView = itemView.findViewById(R.id.image2);
        }
    }
}
